package ch.fhnw.ams.sudoku;

import java.io.File;
import java.io.FileFilter;
import java.util.Arrays;

/**
 * Contains miscellaneous utility methods.
 * 
 * @author amsler
 *
 */
public final class SudokuUtil {
	
	/**
	 * Copies the specified array.
	 * 
	 * @param original
	 *            the array to copy,
	 * @return a deep copy of the original array
	 */
	public static int[][] copyArray(int[][] original) {
		int[][] copy = new int[original.length][];
		
		for (int i = 0; i < original.length; i++) {
			copy[i] = Arrays.copyOf(original[i], original[i].length);
		}
		
		return copy;
	}
	
	/**
	 * Returns a string representation of the specified array. e.g.
	 * 
	 * <pre>
	 * 	1 2 3
	 * 	4 5 6
	 * 	7 8 9
	 * </pre>
	 * 
	 * @param data
	 *            the sudoku array
	 * @return a Sudoku string representation of data
	 */
	public static String toSudokuString(int[][] data) {
		StringBuffer buf = new StringBuffer();
		
		for (int j = 0; j < data.length; j++) {
			for (int i = 0; i < data[j].length; i++) {
				int num = data[j][i];
				if(num == 0) {
					buf.append("- ");
				} else {
					buf.append(num + " ");
				}
			}
			buf.append("\n");
		}
		
		return buf.toString();
	}
	
	/**
	 * Returns a filter for files with {@link Globals#FILE_SUFFIX}
	 * 
	 * @return
	 */
	public static FileFilter getSideKickFileFilter() {
		return new FileFilter() {
			
			@Override
			public boolean accept(File pathname) {
				return pathname.isFile() 
						&& pathname.toString().endsWith(Globals.FILE_SUFFIX);
			}
		};
	}
}
